//
//  DetailViewController.m
//  味·道
//
//  Created by 刘孝纯 on 15/7/1.
//  Copyright (c) 2015年 WMR. All rights reserved.
//

#import "DetailViewController.h"
#import "MaocHeader.h"
#import "AppDelegate.h"
#import <UIImageView+WebCache.h>
@interface DetailViewController ()
@property(nonatomic,retain)UIWebView *webView;
@property(nonatomic,retain)UIImageView *imageView;
@property(nonatomic,retain)UIScrollView *scrollView;
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    [self creatTitle];
    [self creatButton];
    [self creatWebView];
//    [self handleSingleTap:<#(UITapGestureRecognizer *)#>]
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)creatButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.backgroundColor = [UIColor redColor];
    button.frame = CGRectMake(15, 30, 20, 18);
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
    [self.view addSubview:button];
}

-(void)creatTitle
{
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(150, 30, 80, 20)];
    lable.font = [UIFont systemFontOfSize:18.0f];
    [lable setTextColor:[UIColor whiteColor]];
    lable.text = @"详细做法";
    lable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lable];
    [lable release];
}
-(void)creatWebView
{
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-60)];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",self.ID]]]];
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    gesture.delegate = self;
    [self.webView addGestureRecognizer:gesture];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.scrollView.frame = self.webView.frame;
    self.webView.delegate = self;
    self.webView.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    [self.webView release]; 
}
-(void)back:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
//添加轻拍手势，判断是否点击的图片
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
-(void)handleSingleTap:(UITapGestureRecognizer *)sender
{
    CGPoint pt = [sender locationInView:self.webView];//获取在webView的点击坐标
    NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];//判断
    NSString *urlToSave = [self.webView stringByEvaluatingJavaScriptFromString:imgURL];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
 
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlToSave]];
    self.imageView.alpha = 1;
    [self.view addSubview:self.imageView];
    
    
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //判断是否是单击
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        NSURL *url = [request URL];
        NSString *str = [NSString stringWithFormat:@"%@",url];
        
        NSString *str2 = [str substringFromIndex:9];
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        detailVC.ID = str2;
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }
        return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
